﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Secure.Tests.Client
{
    [TestFixture]
    public class ServiceLocatorFixture
    {
        [Test]
        public void should_get_the_uri_for_a_service()
        {
            IServiceLocator locator = new ServiceLocator("http://localhost/locator");
            locator.AddLocationOverride("SecurityGateway", "http://localhost/securityGateway");

            string serviceLocation = locator.GetServiceUri("SecurityGateway");

            Assert.AreEqual("http://localhost/securityGateway", serviceLocation);

            serviceLocation = locator.GetServiceUri("UnknownService");

            Assert.IsNull(serviceLocation);
        }
    }

    public class ServiceLocator : IServiceLocator
    {
        private readonly string _locatorServiceUri;
        private readonly Dictionary<string, string> _locationOverrides = new Dictionary<string, string>();

        public ServiceLocator(string locatorServiceUri)
        {
            _locatorServiceUri = locatorServiceUri;
        }

        public void AddLocationOverride(string serviceId, string serviceUri)
        {
            if (_locationOverrides.ContainsKey(serviceId))
            {
                throw new ArgumentException("Duplicate service id.", "serviceId");
            }

            _locationOverrides.Add(serviceId, serviceUri);
        }

        public string GetServiceUri(string serviceId)
        {
            string serviceUri;
            _locationOverrides.TryGetValue(serviceId, out serviceUri);

            return serviceUri;
        }
    }

    public interface IServiceLocator
    {
        void AddLocationOverride(string serviceId, string serviceUri);
        string GetServiceUri(string serviceId);
    }
}
